package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.HospitalDepartDto;
import net.zigin.nhi.could.waste.service.HospitalDepartService;
import net.zigin.nhi.could.waste.vo.HospitalDepartVo;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * HospitalDepart控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: HospitalDepartController
 * @date 2021-9-8 0:01:02
 */
@Api(tags = "科室管理")
@RestController
@RequestMapping("/hospitalDepart")
public class HospitalDepartController extends BaseController {
    @Autowired
    private HospitalDepartService hospitalDepartService;

    /**
     * 新增编辑医院科室
     *
     * @param hospitalDepartDto 科室信息
     * @return 科室ID
     */
    @ApiOperation("新增编辑医院科室")
    @ApiImplicitParam(name = "hospitalDepartDto", value = "科室信息", required = true, dataType = "HospitalDepartDto")
    @PostMapping("/save")
    public JsonForResult<String> save(@RequestBody HospitalDepartDto hospitalDepartDto) {
        String id = hospitalDepartService.saveOrUpdate(hospitalDepartDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 医院科室列表
     *
     * @param hospitalDepartDto 科室信息
     * @return 医院科室列表
     */
    @ApiOperation("医院科室列表")
    @ApiImplicitParam(name = "hospitalDepartDto", value = "科室信息", required = true, dataType = "HospitalDepartDto")
    @PostMapping("/getList")
    public JsonForResult<List<HospitalDepartVo>> getList(@RequestBody HospitalDepartDto hospitalDepartDto) {
        List<HospitalDepartVo> list = hospitalDepartService.getList(hospitalDepartDto);
        Integer count = hospitalDepartService.getCount(hospitalDepartDto);
        return JsonForResult.<List<HospitalDepartVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 医院科室详情
     *
     * @param id 医院ID
     * @return 医院科室详情
     */
    @ApiOperation("医院科室详情")
    @ApiImplicitParam(name = "id", value = "科室id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<HospitalDepartVo> getById(@PathVariable String id) {
        HospitalDepartVo hospitalDepartVo = hospitalDepartService.getInfoById(id);
        return JsonForResult.<HospitalDepartVo>builder().data(hospitalDepartVo).success(true).build();
    }

    /**
     * 删除医院科室
     *
     * @param id 医院ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除医院科室")
    @ApiImplicitParam(name = "id", value = "科室id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        if(hospitalDepartService.judgeRemove(id)){
            return JsonForResult.builder().message("该科室下存在下级科室或人员信息，不能删除！").success(false).build();
        }
        hospitalDepartService.deleteByPrimaryKey(id);
        return JsonForResult.builder().success(true).build();
    }

    /**
     * 下载二维码
     *
     * @param id       科室ID
     * @param response
     * @return
     */
    @LoginWhitePathAnnotation
    @GetMapping("/generateQrCode/{id}")
    @ApiOperation("下载二维码")
    @ApiImplicitParam(name = "id", value = "科室id", required = true, dataType = "String")
    public JsonForResult generateQrCode(@PathVariable String id, HttpServletResponse response) {
        hospitalDepartService.generateQrCode(id, response);
        return JsonForResult.builder().success(true).build();
    }

    @ApiOperation("导出科室列表")
    @ApiImplicitParam(name = "hospitalDepartDto", value = "科室信息", required = true, dataType = "HospitalDepartDto")
    @PostMapping("/exportList")
    public void exportList(@RequestBody HospitalDepartDto hospitalDepartDto, HttpServletResponse response) {
        hospitalDepartService.exportList(hospitalDepartDto, response);
    }

    @ApiOperation("导入科室列表")
    //@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    @PostMapping("/importList")
    public JsonForResult testImport(@ApiParam(name = "file",value = "file", required = true) MultipartFile file) {
        hospitalDepartService.importList(file);
        return JsonForResult.builder().success(true).build();
    }

    @LoginWhitePathAnnotation
    @ApiOperation("下载模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        hospitalDepartService.downloadTemplate2(response);
    }

    @PostMapping("/generateQrCode")
    @ApiOperation("批量下载二维码")
    @ApiImplicitParam(name = "ids", value = "科室id列表", required = true, allowMultiple = true, dataType = "String")
    public void generateQrCode(@RequestBody List<String> ids, HttpServletResponse response) {
        hospitalDepartService.generateQrCode(ids, response);
    }

    @GetMapping("test")
    public String fillPinYinName(){
        hospitalDepartService.fillPinYinName();
        return "success";
    }

}
